
package com.example.testutil.util.file;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xmlpull.v1.XmlPullParser;

import android.util.Xml;

import com.example.testutil.util.LogUtil;

/*
 * 参考链接http://www.jianshu.com/p/e636f4f8487b
 */
public class XMLparse {
    public static void parseWithDOM(InputStream stream) {
        DocumentBuilderFactory mBuilderFactory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder mBuilder = mBuilderFactory.newDocumentBuilder();
            org.w3c.dom.Document document = mBuilder.parse(stream);
            // 得到XML的"根节点"
            Element mElement = document.getDocumentElement();
            // 得到根节点上面所有的language节点
            NodeList mList = mElement.getElementsByTagName("language");
            LogUtil.d("==================" + mList.getLength());
            for (int i = 0; i < mList.getLength(); i++) {
                LanguageBean mBean = new LanguageBean();
                Element mChildElement = (Element) mList.item(i);
                LogUtil.d("-----" + mChildElement.getAttribute("id") + "---------"
                        + mChildElement.getElementsByTagName("name").item(0).getTextContent() + "--------"
                        + mChildElement.getElementsByTagName("usage").item(0).getTextContent());
            }
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void XMLparseWithPULL(InputStream inputStream) {
        LanguageBean mBean = null;
        List<LanguageBean> mBeans = null;
        try {
            XmlPullParser mParser = Xml.newPullParser();
            mParser.setInput(inputStream, "utf-8");
            int eventType = mParser.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                switch (eventType) {
                    case XmlPullParser.START_DOCUMENT:
                        mBeans = new ArrayList<LanguageBean>();
                        break;
                    case XmlPullParser.START_TAG:
                        String name = mParser.getName();
                        LogUtil.d("-----------------------"+name);
                        if (name.equals("language")) {
                            mBean = new LanguageBean();
                            mBean.id = Integer.valueOf(mParser.getAttributeValue(0));
                        } else if (name.equals("name")) {
                            mBean.name = mParser.nextText();
                        } else if (name.equals("usage")) {
                            mBean.usage = mParser.nextText();
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        if ("language".equals(mParser.getName())) {
                            mBeans.add(mBean);
                            LogUtil.d("-----------" + mBean.toString());
                        }
                        break;
                    case XmlPullParser.END_DOCUMENT:
                        break;
                    default:
                        break;
                }
                eventType = mParser.next();
            }
        } catch (Exception e) {
        }
    }
}
